<template>
  <div>
    <el-form ref="ruleForm" :model="form" style="margin: 20px">
      <!-- 顶部 -->
      <el-form-item>
        <el-row>
          <el-col :span="3.5">
            <el-input
              v-model="ruleForm.userId"
              placeholder="请输入用户ID"
              clearable
            ></el-input>
          </el-col>
          <el-col :span="3.5">
            <el-input
              v-model="ruleForm.goodsId"
              placeholder="请输入商品ID"
              clearable
            ></el-input>
          </el-col>
          <el-button type="primary" icon="el-icon-search" @click="find"
            >查找</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-download"
            @click="handleDownload"
            >导出</el-button
          >
        </el-row>
      </el-form-item>
      <!-- 列表 -->
      <el-table :data="tableData" border style="width: 100%" class="tables">
        <el-table-column prop="id" label="足迹ID" sortable width="100">
        </el-table-column>
        <el-table-column prop="userId" label="用户ID" width="447">
        </el-table-column>

        <el-table-column prop="goodsId" label="商品ID" width="446">
        </el-table-column>

        <el-table-column prop="addTime" label="添加时间" width="446">
        </el-table-column>
      </el-table>
      <pagination
        :total="total"
        :page="ruleForm.page"
        @size="setSize"
        @current="setCurrent"
        style="margin-top: 30px; padding: 32px 16px"
      ></pagination>
    </el-form>
  </div>
</template>

<script>
import { fetchList } from "@/api/footprint";
import pagination from "../pagination.vue";
import * as XLSX from "xlsx";
export default {
  data() {
    return {
      input: "",
      total: 0,
      form: {
        name: "",
      },
      tableData: [],
      ruleForm: {
        userId: "",
        goodsId: "",
        page: 1,
        limit: 20,
        total: 0,
      },
    };
  },
  // 注册分页标签
  components: {
    pagination,
  },
  mounted() {
    this.getfetchList();
    this.find();
  },
  methods: {
    // 渲染列表
    getfetchList() {
      fetchList(this.ruleForm).then((res) => {
        console.log(res, "耶");
        if (res.data.errno == 0) {
          this.tableData = res.data.data.list;
          this.total = res.data.data.total;
        }
      });
    },
    // 查找
    find() {
      this.getfetchList();
    },
    // 分页
    setSize(val) {
      console.log(`${val}`, "条数");
      this.ruleForm.page = 1;
      this.ruleForm.limit = val;
      this.getfetchList();
    },

    setCurrent(val) {
      console.log(`${val}`, "当前页");
      this.ruleForm.page = val;
      this.getfetchList();
    },
    // 导出
    handleDownload() {
      let demo = [];
      for (let i = 0; i < this.tableData.length; i++) {
        let obje = {
          用户名ID: this.tableData[i].userId,
          商品ID: this.tableData[i].goodsId,
          添加时间: this.tableData[i].addTime,
        };
        demo.push(obje);
      }
      console.log(demo);
      const worksheet = XLSX.utils.json_to_sheet(demo); //这里面传入要导出的表格数据
      const workbook = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(workbook, worksheet, "data"); //data为自定义表格名字
      XLSX.writeFile(workbook, "用户收藏信息.xlsx"); //one.xlsx指的是文件名
    },
  },
};
</script>

<style scoped lang="scss">
/* 表格文字居中 */
::v-deep .cell {
  text-align: center;
}
</style>